#include"DemoRayCast.h"
#include"LSYEasyBody.h"
#include"LSYEasyWorld.h"
#include"GLES-Render.h"
#include"Tool.h"
#include<functional>
#include<cmath>

USING_NS_CC;

bool DemoRayCast::init()
{
	if (!AbstractLayer::init(b2Vec2(0, -10)))
	{
		return false;
	}
	return true;
}

void DemoRayCast::box2DAppReady()
{
	auto size = Director::getInstance()->getWinSize();
	LSYEasyBody::createRectangle(b2Vec2(0, 0), size.width, size.height);
	createBody();
	point1.Set(250 * PTM_RADIO, 450 * PTM_RADIO);
	point2.SetZero();
}

void DemoRayCast::createBody()
{
	LSYEasyBody::createBox(b2Vec2(250, 400), b2Vec2(300, 100), b2BodyType::b2_kinematicBody);
	LSYEasyBody::createBox(b2Vec2(250, 50), b2Vec2(300, 100),b2BodyType::b2_kinematicBody)->SetUserData((void*)&USER_DATA_GROUND);
	
	player = LSYEasyBody::createCircle(b2Vec2(50, 250), 15);
	player->GetFixtureList()->SetFriction(0);
	player->SetLinearDamping(1);
	player->SetAngularDamping(1);
	player->GetFixtureList()->SetRestitution(0);
	player->SetUserData((void*)&USER_DATA_PLAYER);
}

void DemoRayCast::update(float dt)
{
	AbstractLayer::update(dt);
	
	moveRay();
	pWorld->RayCast(this, point1, point2);

	if (closetBody != nullptr&&closetBody->GetUserData() == (&USER_DATA_PLAYER))
	{
		isCircle = true;
		cpos = closetBody->GetPosition();
		std::function<void(float)> func = [this](float) {
			isCircle = false;
		};
		scheduleOnce(func, 1, "aaa");
	}
	movePlayer();
}

void DemoRayCast::moveRay()
{
	if (rayAngle < ANGLE_MIN)
	{
		angle_speed *= -1;
		rayAngle = ANGLE_MIN;
	}
	else if (rayAngle > ANGLE_MAX)
	{
		angle_speed *= -1;
		rayAngle = ANGLE_MAX;
	}
	rayAngle += angle_speed;

	point2.x = point1.x - rayLength * cosf(rayAngle);
	point2.y = point1.y - rayLength * sinf(rayAngle);
}

void DemoRayCast::onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event * event)
{
	if (keyCode == EventKeyboard::KeyCode::KEY_LEFT_ARROW)
	{
		isMove = true;
		m_speed = -abs(m_speed);
	}
	else if (keyCode == EventKeyboard::KeyCode::KEY_RIGHT_ARROW)
	{
		isMove = true;
		m_speed = abs(m_speed);
	}
	else if (keyCode == EventKeyboard::KeyCode::KEY_UP_ARROW)
	{
		player->ApplyLinearImpulse(b2Vec2(0, 8), player->GetWorldCenter(), true);
	}
}

void DemoRayCast::onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event * event)
{
	if (keyCode == EventKeyboard::KeyCode::KEY_LEFT_ARROW)
	{
		isMove = false;
	}
	else if (keyCode == EventKeyboard::KeyCode::KEY_RIGHT_ARROW)
	{
		isMove = false;
	}
}

float32 DemoRayCast::ReportFixture(b2Fixture * fixture, const b2Vec2 & point, const b2Vec2 & normal, float32 fraction)
{
	closetPoint = point;
	closetBody = fixture->GetBody();
	return fraction;
}

void DemoRayCast::draw(cocos2d::Renderer * renderer, const cocos2d::Mat4 & transform, uint32_t flags)
{
	AbstractLayer::draw(renderer, transform, flags);
	pDebug->DrawSegment(point1, point2, red);
	if (isCircle)
	{
		pDebug->DrawCircle(cpos, cradius, red);
	}
}

void DemoRayCast::movePlayer()
{
	if (isMove)
	{
		b2Vec2 vec = player->GetLinearVelocity();
		player->SetLinearVelocity(b2Vec2(m_speed, vec.y));
	}
}
